import * as XLSX from 'xlsx'
import { useCallback } from 'react'
function useFileButton(fileRef) {
    const selectExcel = useCallback(() => {
        fileRef.current.input.click()
    }, [fileRef])

    const uploadFile = () => {
        const file = fileRef.current.input.files[0]
        const reader = new FileReader()
        reader.readAsBinaryString(file)
        return new Promise(resolve => {
            reader.onload = function () {
                const workbook = XLSX.read(this.result, { type: 'binary' })
                const t = workbook.Sheets['工作表1']
                const r = XLSX.utils.sheet_to_json(t)
                resolve(r)
            }
        })
    }
    return {
        selectExcel,
        uploadFile
    }
}

export default useFileButton